package com.shijianchen.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.HashMap;

public class QueryUtils<T,D> {
    public QueryWrapper<D> getWrapper(T instance) throws IllegalAccessException {
        HashMap<String, T> map = RevokeUtils.revokeBecomeMap(instance);
        QueryWrapper<D> wrapper = new QueryWrapper<>();
        return wrapper.allEq(map);
    }
    public QueryWrapper<D> getWrapper(T instance,String[] likes) throws IllegalAccessException {
        HashMap<String, T> map = RevokeUtils.revokeBecomeMap(instance);
        QueryWrapper<D> wrapper = new QueryWrapper<>();
        for(String key : likes){
            T value = map.remove(key);
            if (value instanceof String) {
                if (!((String) value).isEmpty()) {
                    wrapper.like(key,"%"+value+"%");
                }
            } else {
                if (value != null) {
                    wrapper.like(key,"%"+value+"%");
                }
            }
        }
        return wrapper.allEq(map);
    }
}
